package com.ruoyi.douyin.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.vo.AuthVo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.douyin.service.AuthService;
import com.ruoyi.douyin.vo.AddMonitorVideoVo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.DouyinUser;
import com.ruoyi.system.domain.MonitorVideo;
import com.ruoyi.system.domain.MonitorVideoDetail;
import com.ruoyi.system.service.MonitorVideoDetailService;
import com.ruoyi.system.service.MonitorVideoService;
import com.ruoyi.web.controller.tool.AuthenCheckServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * (MonitorVideo)表控制层
 *
 * @author makejava
 * @since 2020-10-15 15:20:08
 */
@RestController
@RequestMapping("/monitorVideo")
public class MonitorVideoController {
    /**
     * 服务对象
     */
    @Resource
    private MonitorVideoService monitorVideoService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private AuthService authService;
    @Autowired
    private MonitorVideoDetailService monitorVideoDetailService;
    @Autowired
    private TokenService tokenService;

    /**
     * 添加数据
     *
     * @param addMonitorVideoVo 要添加的监控的数据
     * @return
     */
    @PostMapping("/save")
    public AjaxResult addMonitorVideo(@RequestBody AddMonitorVideoVo addMonitorVideoVo) {

        String itemId = addMonitorVideoVo.getMonitorVideo().getItem_id();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        int total = monitorVideoService.isBind(itemId).size();
        if (total > 0) return AjaxResult.error("不可重复添加");
        AuthVo authVo = redisCache.getCacheObject(addMonitorVideoVo.getOpenId());
        if (authVo == null) {
            return AjaxResult.error("授权过期");
        }
        AjaxResult result = authService.userInfo(Collections.singletonList(addMonitorVideoVo.getOpenId()));
        List list = (List) result.get("data");
        DouyinUser map = (DouyinUser) (list.get(0));
        // Map map = JSON.parseObject(user.toString(), Map.class);
        monitorVideoService.addMonitorVideo(
                addMonitorVideoVo.getMonitorVideo(),
                addMonitorVideoVo.getOpenId(),
                map.getNickname(),
                loginUser.getUser().getUserId().toString());
        return AjaxResult.success();
    }

    @GetMapping("/list/{current}")
    public AjaxResult monitorVideoList(@PathVariable("current") Integer current) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        PageHelper.startPage(current, 10);
        List<MonitorVideo> monitorVideos = monitorVideoService
                .selectAllByUserId(loginUser.getUser().getUserId().toString());
        return AjaxResult.success("success", PageInfo.of(monitorVideos));
    }

    /**
     * 视频监控的详情列表
     *
     * @param itemId
     * @return
     */
    @PostMapping("/list/details")
    public AjaxResult monitoryVideoDetailList(@RequestBody Map<String, Object> itemId) {
        //PageHelper.startPage(index, 10);
        List<MonitorVideoDetail> items = monitorVideoDetailService
                .monitoryVideoDetailList(itemId.get("itemId").toString());
        return AjaxResult.success(items);
    }
}
